package com.ruoyi.hikvision.handler;

import com.ruoyi.hikvision.domain.HikvisionDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DeviceHandler {

    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;


    /**
     * 设备下发人脸
     * @param deviceInfo
     */
    @Async
    public void download(HikvisionDevice deviceInfo){
        // 上锁
        setLock(deviceInfo.getDeviceNo());
        // 登录设备下发人脸
        //faceManage.download(deviceInfo);
        // 删除锁
        destroyLock(deviceInfo.getDeviceNo());
    }

    /**
     * 获取锁状态
     * @param deviceNo
     * @return
     */
    public Boolean getLock(String deviceNo) {
        Boolean lock = redisTemplate.hasKey(deviceNo);
        if (lock) {
            setLock(deviceNo);
        }
        return lock;
    }

    /**
     * 上锁
     * @param deviceNo
     */
    public void setLock(String deviceNo) {
        redisTemplate.opsForValue().set(deviceNo, deviceNo, 5, TimeUnit.MINUTES);
    }

    /**
     * 销毁锁
     * @param deviceNo
     */
    public void destroyLock(String deviceNo) {
        redisTemplate.delete(deviceNo);
    }
}
